using Org.Loon.Framework.Xna.Game.Core;
using Org.Loon.Framework.Xna.Game.Action.Map;
using System.Collections.Generic;
using Org.Loon.Framework.Xna.Game.Core.Graphics.OpenGL;
using Org.Loon.Framework.Xna.Game.Action.IMovie;

namespace Org.Loon.Framework.Xna.Game.Action.IMovie
{

    public class Movie : LRelease
    {

        private string name;

        private Story story;

        private List<MovieSequence> sequences = new List<MovieSequence>();

        private int currentSequence = 0;

        private bool playing = false;

        private LTexture frameImage = null;

        public Movie(string name, Story story)
        {
            this.name = name;
            this.story = story;
        }

        public void AddMovieSequence(MovieSequence sequence)
        {
            this.sequences.Add(sequence);
            sequence.SetMovie(this);
        }

        public MovieSequence GetMovieSequence(int index)
        {
            return (MovieSequence)this.sequences[index];
        }

        public MovieSequence GetCurrentMovieSequence()
        {
            return (MovieSequence)this.sequences[this.currentSequence];
        }

        public void Start()
        {
            this.playing = true;
            if (this.currentSequence == 0)
                GetCurrentMovieSequence().Start();
        }

        public void Stop()
        {
            this.playing = false;
        }

        public void Handle()
        {
            if (this.playing)
            {
                MovieSequence seq = GetCurrentMovieSequence();
                CheckForNextSequence();
                seq.NextFrame();
            }
        }

        public string GetName()
        {
            return this.name;
        }

        public Story GetStory()
        {
            return this.story;
        }

        public void SetStory(Story story)
        {
            this.story = story;
        }

        public bool IsFinished()
        {
            return (GetCurrentMovieSequence().IsFinished())
                    && (this.currentSequence == this.sequences.Count - 1);
        }

        public int CountMovieSequences()
        {
            return this.sequences.Count;
        }

        public int CountFrames()
        {
            int frames = 0;
            for (int i = 0; i < CountMovieSequences(); i++)
            {
                frames += GetMovieSequence(i).CountFrames();
            }
            return frames;
        }

        public LTexture GetFrameImage()
        {
            return this.frameImage;
        }

        public void SetFrameImage(LTexture frameImage)
        {
            this.frameImage = frameImage;
        }

        public void SkipSequence()
        {
            GetCurrentMovieSequence().Finish();
            CheckForNextSequence();
        }

        private void CheckForNextSequence()
        {
            MovieSequence seq = GetCurrentMovieSequence();
            if ((seq.IsFinished())
                    && (this.currentSequence < this.sequences.Count - 1))
            {
                this.currentSequence += 1;
                seq = GetCurrentMovieSequence();
                seq.Start();
            }
            if ((seq.IsFinished())
                    && (this.currentSequence == this.sequences.Count))
            {
                Stop();
                return;
            }
        }

        public void Forward(int frames)
        {
            GetCurrentMovieSequence().Forward(frames);
        }

        public void Dispose()
        {
            foreach (MovieSequence m in sequences)
            {
                if (m != null)
                {
                    m.Dispose();
                }
            }
            sequences.Clear();
        }
    }
}
